<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\property\system\affiche;

use think\App;
use app\controller\property\Base;
use think\exception\ValidateException;
use app\validate\system\affiche\AfficheCateValidate;
use app\common\repositories\system\affiche\AfficheCateRepository;

class AfficheCate extends Base
{
    protected $repository;

    public function __construct(App $app, AfficheCateRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    /**
     * 分类列表
     * @return string|\think\response\Json
     * @throws \Exception
     *
     * @author Amos
     * 2024-01-30 19:12
     */
    public function list()
    {
        if ($this->request->isAjax()) {
            $where = $this->request->param([
                'keywords' => ''
            ]);
            [$page, $limit] = $this->getPage();
            $data = $this->repository->getpropertyList($where, $page, $limit, $this->request->propertyId);
            return json()->data(['code' => 0, 'data' => $data['list'], 'count' => $data['count']]);
        }
        return $this->fetch('system/affiche/cate/list', [
            'addAuth' => property_auth('propertySystemAfficheCateAdd'),
            'editAuth' => property_auth('propertySystemAfficheCateEdit'),
            'delAuth' => property_auth('propertySystemAfficheCateDel'),
            'detailAuth' => property_auth('propertySystemAfficheDetail'),
        ]);
    }

    /**
     *  添加分类
     * @return string|\think\response\Json|\think\response\View
     * @throws \Exception
     *
     * @author Amos
     * 2024-01-30 19:12
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'name' => '',
                'keywords' => '',
                'desc' => '',
                'is_show' => '',
                'sort' => '',
            ]);
            validate(AfficheCateValidate::class)->scene('add')->check($param);
            try {
                $res = $this->repository->addInfo($param, $this->request->propertyId, $this->request->userId);
                if ($res) {
                    return $this->success('添加成功');
                } else {
                    return $this->error('添加失败');
                }
            } catch (\Exception $e) {
                exception_log('添加公告分类失败', $e);
                return $this->error('网络失败');
            }
        }
        return $this->fetch('system/affiche/cate/add');
    }

    /**
     * 编辑分类
     * @return string|\think\response\Json|\think\response\View
     * @throws \Exception
     *
     * @author Amos
     * 2024-01-30 19:11
     */
    public function edit()
    {
        $id = (int)$this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        $info = $this->repository->get($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'name' => '',
                'keywords' => '',
                'desc' => '',
                'is_show' => '',
                'sort' => '',
            ]);
            validate(AfficheCateValidate::class)->scene('edit')->check($param);
            try {
                $res = $this->repository->editInfo($info, $param, $this->request->userId);
                if ($res) {
                    return $this->success('修改成功');
                } else {
                    return $this->error('修改失败');
                }
            } catch (\Exception $e) {
                exception_log('编辑公告分类失败', $e);
                return $this->error('网络错误');
            }
        }
        return $this->fetch('system/affiche/cate/edit', [
            'info' => $info
        ]);
    }

    /**
     * 删除分类
     * @return string|\think\response\Json|\think\response\View
     *
     * @author Amos
     * 2024-01-30 19:11
     */
    public function del()
    {
        $ids = (array)$this->request->param('ids');
        try {
            $data = $this->repository->batchDelete($ids, $this->request->userId);
            if ($data) {
                return $this->success('删除成功');
            } else {
                return $this->error('删除失败');
            }
        } catch (\Exception $e) {
            exception_log('删除公告分类失败', $e);
            return $this->error('网络失败');
        }
    }

    /**
     * 分类详情
     * @return string|\think\response\Json|\think\response\View
     * @throws \Exception
     *
     * @author Amos
     * 2024-01-30 19:10
     */
    public function detail()
    {
        $id = (int)$this->request->param('id');
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        $info = $this->repository->getDetail($id);
        if ($info['id'] <= 0) {
            return $this->error('信息不存在');
        }
        return $this->fetch('system/affiche/cate/detail', [
            'info' => $info,
        ]);
    }

}